Skip to content

fix(paginate): break loop on empty items with non-null nextToken#48

Open
aldorizona10-glitch wants to merge 2 commits into
TestSprite:mainfrom
aldorizona10-glitch:fix/paginate-empty-items-infinite-loop
Open

fix(paginate): break loop on empty items with non-null nextToken#48
aldorizona10-glitch wants to merge 2 commits into
TestSprite:mainfrom
aldorizona10-glitch:fix/paginate-empty-items-infinite-loop

Conversation

@aldorizona10-glitch

Copy link
Copy Markdown

Summary

Fixes an infinite loop in paginate() (issue #35) where the CLI hangs indefinitely when a filtered list endpoint returns { items: [], nextToken: 'cursor' }.

Problem

paginate() in src/lib/pagination.ts checked nextToken !== null to decide whether to keep looping, but never checked whether items was actually empty. Without --max-items set, the loop had no way to stop for this edge case, causing testsprite test list --project <id> with a zero-result filter to hang forever.

Fix

Added an early-exit guard after processing each page:

// Guard against infinite loop when API returns empty items with non-null nextToken.
if (page.items.length === 0) break;

This breaks the loop when the API returns zero items, regardless of what nextToken says. The nextToken from the server is still returned to the caller for potential resume.

Changes

  • src/lib/pagination.ts: Added empty-items guard in paginate() loop
  • test/pagination.test.ts: New file with 5 unit tests:
    • Collects all items across pages until nextToken is null
    • Breaks out of loop on empty items with non-null nextToken (the bug repro)
    • Respects maxItems cap
    • Handles single page with null nextToken
    • Handles consecutive empty pages then data

Testing

  • npm run typecheck — clean
  • npm run lint — clean
  • npm run format:check — all files pass Prettier
  • npm test — all existing tests + 5 new tests pass

Closes #35

Signed-off-by: Aldo Rizona aldorizona10@gmail.com

paginate() in pagination.ts entered an infinite loop when the API
returned { items: [], nextToken: 'cursor' }. The loop only checked
nextToken !== null, never whether items was empty. Any filtered list
command returning zero results would hang indefinitely.

Add an early-exit guard: if the page contains zero items, break
regardless of nextToken. Includes 5 unit tests covering the bug
reproduction, maxItems cap, single-page, and consecutive empty pages.

Closes TestSprite#35

Signed-off-by: Aldo Rizona <aldorizona10@gmail.com>
@aldorizona10-glitch

Copy link
Copy Markdown
Author

Hi maintainers 👋

First-time contributor here — CI runs are awaiting approval ().

Quick summary of this PR:

Coverage impact: this PR adds tests to src/lib/pagination.ts (previously had 14 tests, now 19). The 2 lines added are fully covered. Global coverage should remain ≥80%.

Happy to make any changes if needed. Thanks for reviewing!

…ck gate

Pure mechanical prettier reflow of two vi.fn(async () => ({...})) callbacks in
test/pagination.test.ts. No logic change — only line wrapping to satisfy the
format:check CI gate (npm run format:check). All other gates already pass.
Coverage remains >=80% on all 4 metrics (lines/statements/functions/branches).
@zeshi-du

Copy link
Copy Markdown
Contributor

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@zeshi-du, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 59 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 5daf1369-d7e1-492c-8cd3-48ea04c0fb95

📥 Commits

Reviewing files that changed from the base of the PR and between 15e95de and b7e78c7.

📒 Files selected for processing (2)
  • src/lib/pagination.ts
  • test/pagination.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Infinite loop in paginate() when API returns empty items with non-null nextToken

2 participants